// cheerio模块的爬虫技术 html解析器

let cheerio = require("cheerio");
let http = require("http");
let https = require("https");

// 创建服务器
let server = http.createServer();
server.on("request", (req, res) => {
  switch (req.url) {
    case "/data": {
      res.writeHead(200);
      // GET传入爬虫函数处理数据
      httpsGet(spAct);
      break;
    }
    default:
      break;
  }

  res.end("1");
});

server.listen(3000, () => {
  console.log("3000 port");
});

function httpsGet(callback) {
  // 获取html代码
  let htmlData = "";
  https.get(`https://www.qimao.com/`, (res) => {
    res.on("data", (chunk) => {
      htmlData += chunk;
    });
    res.on("end", () => {
      // 结束时回调
      callback(htmlData);
    });
  });
}

function spAct(data) {
  // 把html数据转化为dom树
  let $ = cheerio.load(data);

  //   根据语义化分析提取数据
  let $itemNameList = $(".right-col");
  $itemNameList.each((index, item) => {
    console.log($(item).find(".s-title.s-title-two").text().trim());
  });
}
